﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour
{
    public float speet = 5;
    CharacterController dd;
    bool iscd = true;
    Animator anima;
    Transform boos;
    bool ismove2 = true;
    float attacktime = 2f;
    public static bool boosNotDied = true;
    public static bool iscd2 = true;
    private void Start()
    {
        anima = this.GetComponent<Animator>();
        //boos = GameObject.FindGameObjectWithTag("enemy").transform;
        boosNotDied = true;
    }
    private void Update()
    {
        if(boos == null)
        {
            if (boosNotDied)
            {
                boos = GameObject.FindGameObjectWithTag("enemy").transform;
            }
            
        }
        else
        {
            if (Vector3.Distance(this.transform.position, boos.position) > 4.5)
            {
                //Debug.Log("gogo");
                this.transform.LookAt(boos);
                transform.position = Vector3.MoveTowards(this.transform.position, boos.position, 5f * Time.deltaTime);
                PlayerMove(true);
                attacktime = 1.5f;
            }
            else
            {
                if (GameUiControl._instance.gameover)
                {
                    anima.SetTrigger("isidle");
                }
                else
                {
                    PlayerAttack();
                    GameUiControl._instance.AttackBtn();
                    PlayerMove(false);
                }
                
            }

        }
    }
    public void PlayerMove(bool ismove)
    {
         anima.SetBool("ismove",ismove);
    }
    public void PlayerAttack()
    {
        ismove2 = false;
        //if (!iscd)
        //    return;
        //Const.isbefore = true;
        //iscd = false;
        anima.SetTrigger("isattack");
        //StartCoroutine(AttackCd());
    }

    IEnumerator AttackCd()
    {
        yield return new WaitForSeconds(0.8f);
        iscd = true;
    }



    public void PlayerHit()
    {
        //anima.SetTrigger("ishit");
    }

    public void PlayerDie()
    {
        anima.SetTrigger("isdie");
    }


    public void AttackHandle()
    {
        ismove2 = true;
    }

    public void DieHandle()
    {
       
    }

}
